/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb;

import entity.Calendar;
import entity.Event;
import entity.User;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author user
 */
@Stateless

public class Calendarejb {

    @PersistenceContext(unitName = "meteocalpersist")
    EntityManager emcalendar, emuser, emevent;

    private Calendar calendar;
    private User user;
    private Event event;
    private List<Event> events;

    private int calendarID;

    public void buildCalendar(User userr) {
        if (userr != null) {
            calendar = new Calendar();
            calendar.setOwner(userr.getUsername());
            calendar.setPrivacy("private");
            emcalendar.persist(calendar);
            user = emuser.find(User.class, userr.getUsername());
            user.setCalendarID(calendar);
            emuser.flush();
        }
    }

    public void loadCalendar(String username) {

        user = emuser.find(User.class, username);
        calendar = user.getCalendarID();
        calendarID = calendar.getId();
        System.out.print("Loaded Calendar ID: " + calendarID);

    }

    public String getOwner() {
        return calendar.getOwner();
    }

    public Calendar getEventsinCalendar(String username) {
        user = emuser.find(User.class, username);
        return calendar = user.getCalendarID();

    }

    public Event findEventbyName(String Nameevent, Date Dateevent) {
        try {
            Query query = emevent.createQuery("SELECT c FROM Event c WHERE c.name = :name AND c.startDateTime = :date");
            query.setParameter("name", Nameevent);
            query.setParameter("date", Dateevent);
            event = (Event) query.getSingleResult();
        } catch (NoResultException e) {
            System.out.println("nel catch");
        }
        return event;
    }

    public List<Event> getAllEvents(String eventsearch) {
        try {
            if (eventsearch != null) {
                Query query = emevent.createQuery("SELECT c FROM Event c WHERE c.name = :name");
                query.setParameter("name", eventsearch);
                System.out.println("query fatta");
                events = (List<Event>) query.getResultList();
                System.out.println("lista trovata");
                for (int i = 0; i < events.size(); i++) {
                    System.out.println("sono nel for");
                    if (events.get(i).getPrivacy().equals("Private")) {
                        events.remove(i);
                    }
                }
                System.out.println("check private events done");
            }
        } catch (NoResultException e) {
            System.out.println("nel catch");
        }

        return events;

    }

}
